# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
# from spmi.dwd.spmi import spmi_dwd__dwd_ibk_increase_day_report_daily_detail_dt

label = f"doris_dwd_ibk_increase_day_report_daily_detail_dt{int(time())}"
timeout = timedelta(minutes=60).seconds
doris_dwd_ibk_increase_day_report_daily_detail_dt = RichSqlSensor(
    task_id='doris_dwd_ibk_increase_day_report_daily_detail_dt',
    pool='broker_load_pool',
    email=['jarl.huang@jtexpress.com','yl_bigdata@yl-scm.com'],
    conn_id='new_doris',
    pre_sql=f"""
    TRUNCATE TABLE spmi_dwd.dwd_ibk_increase_day_report_daily_detail_dt ;
    LOAD LABEL spmi_dwd.{label} (
        DATA INFILE(
                    "hdfs://{{{{var.value.hadoop_namespace}}}}/dw/hive/spmi_dwd.db/external/dwd_ibk_increase_day_report_daily_detail_dt/*"
                    )
        INTO TABLE dwd_ibk_increase_day_report_daily_detail_dt  
    FORMAT AS 'PARQUET'
    )
    WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
    PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM spmi_dwd WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM spmi_dwd WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

# doris_dwd_ibk_increase_day_report_daily_detail_dt << spmi_dwd__dwd_ibk_increase_day_report_daily_detail_dt